package here.android.herehelper;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import android.content.Context;
import android.util.Log;

public class hereSocketServer
{
	private int					port			= 59668;
	private int					server_num		= 3;
	private static ServerSocket	serverSocket	= null;
	private Context				m_CX			= null;
	private final static String	TAG				= hereSocketServer.class
														.toString( );

	public hereSocketServer() throws IOException
	{
		if ( null == serverSocket )
			serverSocket = new ServerSocket(port, server_num);

		Log.i(TAG, "The server is created");
	}

	public void stopServer()
	{
		try
		{
			if ( null != serverSocket )
				serverSocket.close( );
		}
		catch ( IOException e )
		{
			e.printStackTrace( );
		}
	}

	public void startServer()
	{
		boolean continueST = true;

		while ( continueST )
		{
			Socket socket = null;
			try
			{
				socket = serverSocket.accept( );

				Log.i(TAG,
						"new connection is completed "
								+ socket.getInetAddress( ) + ":"
								+ socket.getPort( ));

				// process a client every time is enough
				ProcClient(socket);
			}
			catch ( IOException e )
			{
				continueST = false;
				e.printStackTrace( );
			}
			finally
			{
				if ( socket != null )
				{
					try
					{
						socket.close( );
					}
					catch ( IOException e )
					{
						e.printStackTrace( );
					}
				}
			}
		}
	}

	private void ProcClient(Socket s) throws IOException
	{
		BufferedReader in;
		DataOutputStream out;
		try
		{
			in = new BufferedReader(new InputStreamReader(s.getInputStream( ),
					"UTF-8"));

			// PrintWriter out = new PrintWriter(s.getOutputStream( ));
			out = new DataOutputStream(s.getOutputStream( ));
			while ( !s.isClosed( ) )
			{
				String str;
				str = in.readLine( );

				if ( str == null || str.equals("end") )
					break;

				// Log.d(TAG, str);

				if ( str.equals("GetFreeMem") )
				{
					// out.println("Mem=" +
					// herePhoneInfo.getStrAvailableInternalMemorySize( ));
					// out.flush( );
				}
				else if ( str.equals("GetTotalMem") )
				{
					// out.println("TotalMem=" +
					// herePhoneInfo.getStrTotalMemorySize( ));
					// out.flush( );
				}
				else if ( str.equals("Contact") )
				{
					String outMsg = herePhoneInfo.getContactOne(m_CX);
					// outMsg.
					// String newStr = new String(outMsg.getBytes(), "GB2312");
					byte[] responseBuffer = outMsg.getBytes("GB2312");
					out.write(responseBuffer, 0, responseBuffer.length);
					out.flush( );
					// out.write(bytes);
					// out.println(newStr);
					// out.flush( );
				}
			}
			s.close( );
		}
		catch ( IOException e )
		{
			e.printStackTrace( );
		}

	}

	public void SetAppContext(Context cx)
	{
		m_CX = cx;
	}
}
